﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Salon
{
    class AutoSalonService
    {
        public AutoSalonService() { }

        public AutoSalon saveAutoSalon(AutoSalon autoSalon)
        {
            String insertQuery = "INSERT INTO Salon VALUES(@name,@country,@city,@street,@zip,@flat_number)";

            String updateQuery = "UPDATE Salon SET name=@name,country=@country,city=@city,street=@street,zip=@zip,flat_number=@flat_number WHERE id=@id";

            SqlCommand cmd = SqlService.getInstance().getCommand( (autoSalon.Id < 1) ? insertQuery : updateQuery );
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@name", autoSalon.Name);
            cmd.Parameters.AddWithValue("@country", autoSalon.Country);
            cmd.Parameters.AddWithValue("@city", autoSalon.City);
            cmd.Parameters.AddWithValue("@street", autoSalon.Street);
            cmd.Parameters.AddWithValue("@zip", autoSalon.Zip);
            cmd.Parameters.AddWithValue("@flat_number", autoSalon.FlatNumber);
            if (autoSalon.Id > 0)
                cmd.Parameters.AddWithValue("@id", autoSalon.Id);
            cmd.ExecuteNonQuery();

            return autoSalon;
        }

        //przy usuwaniu ustawiamy flagę active na false
        public void deleteAutoSalon(AutoSalon AutoSalon)
        {
            String query = "DELETE FROM Salon WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", AutoSalon.Id);
            cmd.ExecuteNonQuery();
        }

        //sprawdza czy dany klient już istnieje i zwraca id
        public int checkIfNameExists(String autoSalon_name)
        {
            String query = "Select id FROM Salon WHERE name=@clien_name";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@clien_name", autoSalon_name);
            String id = (String)cmd.ExecuteScalar();

            if (id != null) return Convert.ToInt32(id);
            return 0;
        }

        public AutoSalon getAutoSalonById(int id)
        {
            String query = "SELECT * FROM Salon WHERE id=@id";
            SqlCommand cmd = SqlService.getInstance().getCommand(query);
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@id", id);
            SqlDataReader rdr = cmd.ExecuteReader();
            if (!rdr.HasRows)
                return null;
            rdr.Read();

            AutoSalon AutoSalon = new AutoSalon();
            AutoSalon.Id = rdr.GetInt32(rdr.GetOrdinal("id"));
            AutoSalon.Name = rdr.GetString(rdr.GetOrdinal("name"));
            AutoSalon.Country = rdr.GetString(rdr.GetOrdinal("country"));
            AutoSalon.Street = rdr.GetString(rdr.GetOrdinal("street"));
            AutoSalon.Zip = rdr.GetString(rdr.GetOrdinal("zip"));
            AutoSalon.FlatNumber = rdr.GetString(rdr.GetOrdinal("flat_number"));

            return AutoSalon;
        }

        /// <summary>
        /// Zwraca listę obiektów typu AutoSalon na podstawie listy kontrolek filtrujących
        /// </summary>
        /// <param name="filterList"></param>
        /// <returns></returns>
        public IList<AutoSalon> getAutoSalons(IList<IFilter> filterList)
        {
            IList<AutoSalon> AutoSalons = new List<AutoSalon>();
            String query = "SELECT * FROM Salon WHERE id > 0 ";
            query += Global.getConditionString(filterList);

            SqlDataReader reader = SqlService.getInstance().getCommand(query).ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    AutoSalon AutoSalon = new AutoSalon();
                    AutoSalon.Id = reader.GetInt32(reader.GetOrdinal("id"));
                    AutoSalon.Name = reader.GetString(reader.GetOrdinal("name"));
                    AutoSalon.Country = reader.GetString(reader.GetOrdinal("country"));
                    AutoSalon.City = reader.GetString(reader.GetOrdinal("city"));
                    AutoSalon.Street = reader.GetString(reader.GetOrdinal("street"));
                    AutoSalon.Zip = reader.GetString(reader.GetOrdinal("zip"));
                    AutoSalon.FlatNumber = reader.GetString(reader.GetOrdinal("flat_number"));

                    AutoSalons.Add(AutoSalon);
                }
            }

            reader.Close();

            return AutoSalons;
        }
    }
}
